package com.matchmealpha;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class FriendsListAdapter extends BaseAdapter implements
		OnItemClickListener {

	public static final String FRIEND_USERNAME = "FRIEND_USERNAME";
	private Context context;
	private List<String> friendsList;
	private int selectedPosition;
	private int callerID;
	private int requestcode;

	/**
	 * 
	 * @param context
	 * @param friendsList
	 * @param callerID
	 * @param callerType - 1 - click to open timeline, 2 - click and return result
	 */
	public FriendsListAdapter(Context context, List<String> friendsList,
			int callerID, int callerType) {
		this.context = context;
		this.friendsList = friendsList;
		this.selectedPosition = Adapter.NO_SELECTION;
		this.callerID = callerID;
		this.requestcode = callerType;
		if (callerID == 0) {
			this.callerID = MatchMeTabWidget.FRIENDS;
		}
	}

	public int getSelectedPosition() {
		return this.selectedPosition;
	}

	@Override
	public int getCount() {
		return this.friendsList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.friendsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
		// return this.friendsList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView != null) {
			((FriendView) convertView)
					.setString(this.friendsList.get(position));
		} else {
			convertView = new FriendView(this.context, friendsList
					.get(position));
		}
		return convertView;
	}

	/**
	 * When a store is selected, display flyer list view for that store by
	 * calling FlyerActivity?
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if ( this.requestcode != ShareSettingActivity.GET_FRIEND_REQUEST_CODE ){
			Intent intent3 = new Intent(this.context, TimelineActivity.class);
			intent3.putExtra("back", true);
			intent3.putExtra("callerID", this.callerID);
			intent3.putExtra("relationship", false);
			intent3.putExtra("curruser", friendsList.get(position));
			View view3 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity("TimelineActivity",
							intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view3, this.callerID);
		}
		else {
			Intent in = new Intent();
			in.putExtra(FRIEND_USERNAME, friendsList.get(position));
			((FriendsListActivity)(this.context)).setResult(Activity.RESULT_OK, in);
			((FriendsListActivity)(this.context)).finish();
		}
		
	}
}
